/*
 * XmlParser.c
 *
 *  Created on: Feb 24, 2012
 *      Author: charbelchoueiri
 */
#include "common.h"

XmlNode* XmlParse(XmlNode* pDocument, Reader* pReader, bool bNormalize)
{
	int32 iChar;
	String* sName = StringCreate(1024);
	String* sValue = StringCreate(1024);
	XmlNode* pCurrent;

	//If document is null then create it.
	if (pDocument == null) pDocument = XmlCreateNode(DOCUMENT_NODE, null, null);

	pCurrent = pDocument;


	iChar = ReaderSkipUntilChar(pReader, '<');
	while (iChar != EOF)
	{
		//Read the first value which should be '<'
		ReaderRead(pReader);

		//Peek onto the next value to see what to do next.
		iChar = ReaderPeek(pReader);

		//If XML document declaration, then skip it because this is not handled.
		if (iChar == '?')
		{
			//Skip all <? ... ?> until the last '>'
			ReaderSkipUntilChar(pReader, '>');
		}
		//If found </ then close element tag is next.
		else if (iChar == '/')
		{
			//Skip / and read string name.
			ReaderRead(pReader);

			//Read the name.

			//Read close tag >
			if (ReaderRead(pReader) != '>') { } //TODO: error.

			//If the read close element tag name is not the same as the current element then error.
			if (pCurrent->nodeName == null || StringCompare(pCurrent->nodeName, sName) != 0) { } //TODO: error.

			//Close the element and move to the parent.
			pCurrent = pCurrent->parentNode;
		}
		//If found <! then read comment.
		else if (iChar == '!')
		{
			//Skip !-- and then read until -->
			if (ReaderRead(pReader) != '!' || ReaderRead(pReader) != '-' || ReaderRead(pReader) != '-') { } //TODO: error.

			//Read until -->
			ReaderReadUntilString(pReader, sValue, "-->");

			//Skip -->
			if (ReaderRead(pReader) != '-' || ReaderRead(pReader) != '-' || ReaderRead(pReader) != '>') { } //TODO: error.

			//Create the comment and add it to the current element
			XmlAppendChild(pCurrent, XmlCreateNode(COMMENT_NODE, null, StringCreate(sValue)));
		}
		//Otherwise read an element.
		else
		{
			//Read name.

			if (sName->length == 0) {} //TODO: error.

			//Create the element and append it to the current node.
			pCurrent = XmlAppendChild(pCurrent, XmlCreateNode(ELEMENT_NODE, sName, null));

			//Read the attributes of the element.
			while (iChar != EOF && iChar != '>' && iChar != '/')
			{
				//Read attribute name.

				//Skip white space and read =

				//Skip white space and read quote ' or "

				//Read attribute value.

				//Read quote ' or " and it must match the start quote.

				//Skip white space.
			}

			if (iChar == '>')
			{
				//Skip characters and continue with the current element.
				ReaderRead(pReader);
			}
			else if (iChar == '/')
			{
				//Read / and then check for >
				ReaderRead(pReader);
				if (ReaderRead(pReader) != '>') { } //TODO: error

				//Move back to the parent since this is a close tag.
				pCurrent = pCurrent->parentNode;
			}
			else { } //TODO: Expecting / or > after element attributes. (Line:%i, Column: %i)
		}

		//Skip white space.

		//Read Text between elements and add them the current element.
		//if (ReaderReadUntilChar(Reader* pReader, sValue, '<') > 0)
		{
			//Create a text element or add text to current node value.
		}
	}


	//Clean up.
	StringDelete(sName);
	StringDelete(sValue);

	return pDocument;
}
